//
//  ZZFInputV.m
//  ZZFUCComponent
//
//  Created by quheng on 2017/8/29.
//  Copyright © 2017年 quheng. All rights reserved.
//

#import "BZUCInputV.h"
#import <Masonry/Masonry.h>
//#import "BZUCBundleKit.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import "NSString+NormalRegex.h"
#import <objc/runtime.h>
#import "BZUCLoginService.h"
#import <BZPreHeaderComponent/BZPreHeaderComponent.h>
//#import <BZBaseWidget/BZBaseWidget.h>

@interface BZUCInputV()


@property (nonatomic, weak) BZBaseTimeKeeperBtn *timerBtn;


@end

@implementation BZUCInputV

- (instancetype)init{

    self = [super init];
    if (self) {
        
        [self configureV];
        
    }

    return self;
}


- (void)configureV{
    
    UIImageView *iconV = [[UIImageView alloc]init];
    [self addSubview:iconV];
    self.iconV = iconV;
    
    UILabel *speratorLineV = [[UILabel alloc]init];
    [speratorLineV setBackgroundColor:[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00]];
    [self addSubview:speratorLineV];

    
    UITextField *textF = [[UITextField alloc]init];
    [textF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:textF];
    self.textF = textF;
    
    UIButton *operationBtn = [[UIButton alloc]init];
    [operationBtn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:operationBtn];
    self.operationBtn = operationBtn;
    
    
    BZBaseTimeKeeperBtn *timerBtn = [[BZBaseTimeKeeperBtn alloc]init];
    [timerBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [timerBtn addTarget:self action:@selector(beginTimer:) forControlEvents:UIControlEventTouchUpInside];
    [timerBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    timerBtn.layer.cornerRadius = 8;
    timerBtn.layer.masksToBounds = YES;//(r:0.63 g:0.82 b:0.61 a:1.00)
    timerBtn.backgroundColor = [UIColor colorWithRed:0.63 green:0.82 blue:0.61 alpha:1.00];
    __weak __typeof(& *timerBtn)weakSender = timerBtn;
    [timerBtn setDGTimerButtonWithDuration:60
                              runingColor:nil
                          runingTextColor:[UIColor whiteColor]
                            runingImgName:nil
                                formatStr:@"还剩%zd秒"
                                 buStatus:^(BZStatus status) {
                                     if (status == BUStatusRuning) {
                                         weakSender.userInteractionEnabled = NO;
                                         weakSender.backgroundColor = [UIColor lightGrayColor];
                                     }else if (status == BUStatusCancel){
                                         weakSender.userInteractionEnabled = YES;
                                     }else if (status == BUStatusNone){
                                         
                                     }else if (status == BUStatusFinish){
                                     }
                                 }];
    [self addSubview:timerBtn];
    
    self.timerBtn = timerBtn;
    
    [iconV mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.equalTo(self);
        make.left.equalTo(self);
        make.size.mas_offset(CGSizeMake(20, 20));
    }];
    
    [speratorLineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.height.mas_offset(1);
        make.bottom.equalTo(self);
    }];
    
    [operationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self);
        make.right.equalTo(self);
        make.size.mas_offset(CGSizeMake(20,20));
        
    }];
    
    [textF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconV.mas_right).with.offset(10);
        make.right.equalTo(self);
        make.top.equalTo(self);
        make.bottom.equalTo(speratorLineV.mas_top);
    }];
    
    [timerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_offset(24);
        make.right.equalTo(self).with.offset(-16);
        make.centerY.equalTo(self);
        make.width.mas_offset(0);
    }];
}


- (void)setInputVModel:(BZUCInputVModel *)inputVModel{
    
    if (!inputVModel.textLimit) {
        
        inputVModel.textLimit = 20;
    }

    _inputVModel = inputVModel;
    
    if (inputVModel.iconVWidth) {
        
        [self.iconV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(inputVModel.iconVWidth);
        }];
        
    }else{
    
        [self.iconV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(0);
        }];
    }
    
    
    if (inputVModel.operationBtnWidth) {
        
        [self.operationBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(inputVModel.operationBtnWidth);
        }];
    }else{
        [self.operationBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(0);
        }];
    
    }
    
    self.textF.placeholder = inputVModel.placeholderText;
    self.textF.keyboardType = inputVModel.keyboardType;
    if (inputVModel.shouldShowTextFDeleted) {
         self.textF.clearButtonMode = UITextFieldViewModeWhileEditing;
    }else{
        self.textF.clearButtonMode = UITextFieldViewModeNever;
    }
    
    self.textF.secureTextEntry = inputVModel.seccuretyText;
    self.iconV.image       = inputVModel.iconImage;
    [self.operationBtn setImage:inputVModel.buttonImage forState:UIControlStateNormal];
    [self.operationBtn setImage:inputVModel.buttonSelectedImage forState:UIControlStateSelected];
    
    if (inputVModel.placeholderColor) {
        [self.textF setValue:inputVModel.placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
    }
    if (inputVModel.placeholderFont) {
        [self.textF setValue:inputVModel.placeholderFont forKeyPath:@"_placeholderLabel.font"];
    }
    
    if (inputVModel.titleColor) {
        [self.textF setTextColor:inputVModel.titleColor];
    }
    
    if (inputVModel.titleFont) {
        [self.textF setFont:inputVModel.titleFont];
    }
    
    if (inputVModel.timerBtnW) {
        
        [self.timerBtn mas_updateConstraints:^(MASConstraintMaker *make) {
           
            make.width.mas_offset(inputVModel.timerBtnW);
        }];
        
    }else{
    
        [self.timerBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_offset(0);
        }];
    }
    self.textF.text = inputVModel.contentText;    
    
}

- (void)beginTimer:(BZBaseTimeKeeperBtn *)sender{
    
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(shouldTriggerTimeKeeper:timeKeeper:)]) {
        [self.delegate shouldTriggerTimeKeeper:self timeKeeper:sender];
    }
}



- (void)didClickBtn:(UIButton *)sender{

    self.textF.secureTextEntry = sender.isSelected;
    sender.selected = !sender.isSelected;
}


- (void)textFieldDidChange:(UITextField *)textField{
    
    NSString *toBeString = textField.text;
    
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"]) {
        
        UITextRange *selectedRange = [textField markedTextRange];//获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        //没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position){
            if (toBeString.length > self.inputVModel.textLimit) {
                textField.text = [toBeString substringToIndex:self.inputVModel.textLimit];
            }
        }else{
            if (toBeString.length > self.inputVModel.textLimit) {
                textField.text = [toBeString substringToIndex:self.inputVModel.textLimit];
            }
        }
    }else{
        
        if (toBeString.length > self.inputVModel.textLimit){
            
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.inputVModel.textLimit];
            if (rangeIndex.length == 1){
                textField.text = [toBeString substringToIndex:self.inputVModel.textLimit];
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.inputVModel.textLimit)];
                textField.text     = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    

    if (self.delegate && [self.delegate respondsToSelector:@selector(inputV:textFieldHasChange:)]) {
        
        [self.delegate inputV:self textFieldHasChange:textField];
    }
}


@end
